package com.yxd.bdmap;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.core.TaxiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.search.share.LocationShareURLOption;
import com.baidu.mapapi.search.share.OnGetShareUrlResultListener;
import com.baidu.mapapi.search.share.ShareUrlResult;
import com.baidu.mapapi.search.share.ShareUrlSearch;
import com.baidu.mapapi.utils.route.BaiduMapRoutePlan;
import com.baidu.mapapi.utils.route.RouteParaOption;

import java.util.ArrayList;
import java.util.List;

import overlayutil.DrivingRouteOverlay;
import overlayutil.PoiOverlay;
import overlayutil.TransitRouteOverlay;
import overlayutil.WalkingRouteOverlay;

/**
 * 百度地图
 * 百度定位
 */
public class MainActivity extends Activity implements View.OnClickListener {
    private Button btn_sate_map;
    private Button btn_normal_map;
    private Spinner spin;
    private TextView tv_location;
    private ImageView img_location;
    private EditText et_key;
    private ImageView iv_search;
    private TextView tv_route;
    private ArrayList<String> list;

    private MapView mMapView;
    private BaiduMap bdMap;//百度地图核心操作类
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    private PoiSearch mPoiSearch;//POI检索周边信息实例
    private Overlay textOverlay;//文字覆盖物
    private Overlay markerOverlay;//地图标记物
    private PoiOverlay poiOverlay;//检索标记物
    private BDLocation mCurLocation;//当前位置
    private Marker mCurMarker;
    private boolean isFirst = true;//首次定位
    private ShareUrlSearch mShareUrlSearch;
    private WalkingRouteOverlay walkingRouteOverlay;
    private TransitRouteOverlay transitRouteOverlay;
    private DrivingRouteOverlay drivingRouteOverlay;
    private RoutePlanSearch routePlanSearch;
    // 第三步，创建分享检索监听者；

    private OnGetShareUrlResultListener shareListener = new OnGetShareUrlResultListener() {
        public void onGetPoiDetailShareUrlResult(ShareUrlResult result) {
            //分享POI详情
        }

        public void onGetLocationShareUrlResult(ShareUrlResult result) {
            //分享位置信息
            String url = result.getUrl();
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, url);
            intent.setType("text/plain");
            startActivity(Intent.createChooser(intent, "小东地图分享"));
        }

        @Override
        public void onGetRouteShareUrlResult(ShareUrlResult shareUrlResult) {

        }
    };
    ///公交线路规划 第二步，创建路规划检索监听者；
    private OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
        //步行
        public void onGetWalkingRouteResult(WalkingRouteResult result) {
            if (walkingRouteOverlay != null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay != null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay != null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                Toast.makeText(MainActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
            }
            if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                //result.getSuggestAddrInfo()
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                walkingRouteOverlay = new WalkingRouteOverlay(bdMap);
                WalkingRouteLine line = result.getRouteLines().get(0);//获取第一条规划路径

                tv_route.setText("步行距离：" + line.getDistance() + "米，需要" + line.getDuration() / 60 + "分钟");
                for (int i = 0; i < line.getAllStep().size(); i++) {
                    tv_route.append("\n"+line.getAllStep().get(i).getInstructions());
                }
                TaxiInfo taxiInfo = result.getTaxiInfo();
                if (taxiInfo != null) {
                    tv_route.append("\n打车约");
                    tv_route.setTextColor(Color.RED);
                    tv_route.append(taxiInfo.getTotalPrice() + "");
                    tv_route.setTextColor(Color.BLACK);
                    tv_route.append("元，距离" + taxiInfo.getDistance() + "米，需要" + taxiInfo.getDuration() / 60 + "分钟");
                } else {
                    tv_route.append("\n未找到打车信息");
                }
                walkingRouteOverlay.setData(line);
                walkingRouteOverlay.addToMap();
                walkingRouteOverlay.zoomToSpan();//视图调整到刚好显示整个路线规划的视图
            }

        }

        //公交
        public void onGetTransitRouteResult(TransitRouteResult result) {
            if (walkingRouteOverlay != null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay != null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay != null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                Toast.makeText(MainActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
            }
            if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                //result.getSuggestAddrInfo()
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                transitRouteOverlay = new TransitRouteOverlay(bdMap);
                // bdMap.setOnMarkerClickListener(transitRouteOverlay);
                TransitRouteLine line = result.getRouteLines().get(0);

                tv_route.setText("公交距离：" + line.getDistance() + "米，需要" + line.getDuration() / 60 + "分钟");
                for (int i = 0; i < line.getAllStep().size(); i++) {
                    Log.d("MainActivity", line.getAllStep().get(i).getInstructions() //步行4米,到达浦上大道站
                            // + "--" + line.getAllStep().get(i).getEntrance() //
                            + "--" + line.getAllStep().get(i).getStepType() //WAKLING
                            //+ "--" + line.getAllStep().get(i).getWayPoints()//经纬度起点-终点
                    );
                    tv_route.append("\n"+line.getAllStep().get(i).getInstructions());
                }
                TaxiInfo taxiInfo = result.getTaxiInfo();
                if (taxiInfo != null) {
                    tv_route.append("\n打车约：");
                    tv_route.setTextColor(Color.RED);
                    tv_route.append(taxiInfo.getTotalPrice() + "");
                    tv_route.setTextColor(Color.BLACK);
                    tv_route.append("元，距离" + taxiInfo.getDistance() + "公里");
                } else {
                    tv_route.append("\n未找到打车信息");
                }
                transitRouteOverlay.setData(result.getRouteLines().get(0));
                transitRouteOverlay.addToMap();
                transitRouteOverlay.zoomToSpan();
            }
        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

        }

        //驾车
        public void onGetDrivingRouteResult(DrivingRouteResult result) {
            if (walkingRouteOverlay != null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay != null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay != null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                Toast.makeText(MainActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
            }
            if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                //起终点或途经点地址有岐义，通过以下接口获取建议查询信息
                //result.getSuggestAddrInfo()
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                drivingRouteOverlay = new DrivingRouteOverlay(bdMap);
                bdMap.setOnMarkerClickListener(drivingRouteOverlay);
                DrivingRouteLine line = result.getRouteLines().get(0);

                tv_route.setText("驾车距离：" + line.getDistance() + "米，需要" + line.getDuration() / 60 + "分钟");
                for (int i = 0; i < line.getAllStep().size(); i++) {
                    tv_route.append("\n"+line.getAllStep().get(i).getInstructions());
                }
                TaxiInfo taxiInfo = result.getTaxiInfo();
                if (taxiInfo != null) {
                    tv_route.append("\n打车约：");
                    tv_route.setTextColor(Color.RED);
                    tv_route.append(taxiInfo.getTotalPrice() + "");
                    tv_route.setTextColor(Color.BLACK);
                    tv_route.append("元，距离" + taxiInfo.getDistance() + "公里");
                } else {
                    tv_route.append("\n未找到打车信息");
                }

                drivingRouteOverlay.setData(result.getRouteLines().get(0));
                drivingRouteOverlay.addToMap();
                drivingRouteOverlay.zoomToSpan();
            }

        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

        }

        //骑行
        @Override
        public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
            if (walkingRouteOverlay != null) {
                walkingRouteOverlay.removeFromMap();
                walkingRouteOverlay = null;
            }
            if (transitRouteOverlay != null) {
                transitRouteOverlay.removeFromMap();
                transitRouteOverlay = null;
            }
            if (drivingRouteOverlay != null) {
                drivingRouteOverlay.removeFromMap();
                drivingRouteOverlay = null;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        initDingwei();
        initLocation();
        mLocationClient.start();

        initView();
        initData();
        initListener();
    }

    //初始化百度定位
    public void initDingwei() {
        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);    //注册监听函数
    }

    //初始化百度定位设置
    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，gcj02是中国国家测绘局制定的地理信息系统的坐标系统，设置返回的定位结果坐标系
        int span = 1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，周边信息，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，默认不杀死,定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程
        option.SetIgnoreCacheException(false);//可选，默认false,默认收集，设置是否收集CRASH信息
        option.setEnableSimulateGps(false);//可选，默认false，默认需要设置是否需要过滤gps仿真结果
        mLocationClient.setLocOption(option);
    }

    private void initView() {
        //获取地图控件引用
        spin = (Spinner) findViewById(R.id.spin);
        tv_location = (TextView) findViewById(R.id.tv_location);
        et_key = (EditText) findViewById(R.id.et_key);
        iv_search = (ImageView) findViewById(R.id.iv_search);
        img_location = (ImageView) findViewById(R.id.img_location);
        mMapView = (MapView) findViewById(R.id.bmapView);
        btn_sate_map = (Button) findViewById(R.id.btn_sate_map);
        btn_normal_map = (Button) findViewById(R.id.btn_normal_map);
        tv_route = (TextView) findViewById(R.id.tv_route);
        tv_route.setMovementMethod(ScrollingMovementMethod.getInstance());
        //POI检索
        //第一步 创建POI检索实例
        mPoiSearch = PoiSearch.newInstance();
        //第二步，创建分享检索实例；

        mShareUrlSearch = ShareUrlSearch.newInstance();
        bdMap = mMapView.getMap();
        //公交线路规划第一步，创建公交线路规划检索实例
        routePlanSearch = RoutePlanSearch.newInstance();


    }

    private void initData() {
        try {
            //用这种方式拿配置信息
            ApplicationInfo info = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            Bundle bundle = info.metaData;
            String bdKey = bundle.getString("com.baidu.lbsapi.API_KEY");
            //这种方式拿不到，bundle为null
            // Bundle bundle = getApplication().getApplicationInfo().metaData;
            // String bakey1 = bundle.getString("com.baidu.lbsapi.API_KEY");

            Log.d("yxd---", bdKey);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        list = new ArrayList<>();
        list.add("功能");
        list.add("实时交通图-开");
        list.add("实时交通图-关");
        list.add("百度城市热力图-开");
        list.add("百度城市热力图-关");
        list.add("添加地图标注");
        list.add("移除地图标注");
        list.add("添加文字覆盖物");
        list.add("移除文字覆盖物");
        list.add("显示我的位置");
        list.add("隐藏我的位置");
        spin.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list));
    }


    private void initListener() {
        btn_sate_map.setOnClickListener(this);
        btn_normal_map.setOnClickListener(this);
        iv_search.setOnClickListener(this);
        img_location.setOnClickListener(this);//定位
        //公交线路规划第三步，设置公交线路规划检索监听者；
        routePlanSearch.setOnGetRoutePlanResultListener(listener);
        // 第四步，设置分享检索监听者；
        mShareUrlSearch.setOnGetShareUrlResultListener(shareListener);

        bdMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {//标记物被点击的监听事件
                Log.d("MainActivity", "OnMarkerClickListener..");
                mCurMarker = marker;
                LatLng postion = marker.getPosition();
                View view = View.inflate(MainActivity.this, R.layout.info_window, null);
                ImageView iv_baidu = (ImageView) view.findViewById(R.id.iv_baidu);
                ImageView iv_walk = (ImageView) view.findViewById(R.id.iv_walk);
                ImageView iv_bus = (ImageView) view.findViewById(R.id.iv_bus);
                ImageView iv_car = (ImageView) view.findViewById(R.id.iv_car);
                ImageView iv_share = (ImageView) view.findViewById(R.id.iv_share);
                iv_baidu.setOnClickListener(MainActivity.this);
                iv_walk.setOnClickListener(MainActivity.this);
                iv_bus.setOnClickListener(MainActivity.this);
                iv_car.setOnClickListener(MainActivity.this);
                iv_share.setOnClickListener(MainActivity.this);

                InfoWindow infoWindow = new InfoWindow(view, postion, -90);
                //bdMap.hideInfoWindow();
                bdMap.showInfoWindow(infoWindow);

                return true;
            }
        });

        //第二步，第三步，创建POI检索监听者；设置POI检索监听者；
        mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult result) {
                //获取POI检索结果
                List<PoiInfo> allPoi = result.getAllPoi();
                for (PoiInfo info : allPoi) {
                    Log.d("MainActivity", info.name + "--" + info.phoneNum + "---" + info.address);
                }

                if (poiOverlay == null) {
                    poiOverlay = new PoiOverlay(bdMap);
                } else poiOverlay.removeFromMap();
                poiOverlay.setData(result);
                poiOverlay.zoomToSpan();
                poiOverlay.addToMap();//根据检索result，在地图上显示标记物


            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult result) {
                //获取Place详情页检索结果
            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });


        spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //条目选中 是回调
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1:
                        //开启交通图
                        bdMap.setTrafficEnabled(true);
                        break;
                    case 2:
                        //开启交通图
                        bdMap.setTrafficEnabled(false);
                        break;
                    case 3:
                        //开启百度城市热力图
                        bdMap.setBaiduHeatMapEnabled(true);
                        break;
                    case 4:
                        //开启百度城市热力图
                        bdMap.setBaiduHeatMapEnabled(false);
                        break;
                    case 5:
                        //开启百度城市热力图
                        addMarker(39.963175, 116.400244);
                        break;
                    case 6:
                        //开启百度城市热力图
                        removeMarker();
                        break;
                    case 7:
                        addText(39.963175, 116.400244);
                        break;
                    case 8:
                        removeText();
                        break;
                    case 9:
                        showMyLocation();
                        break;
                    case 10:
                        hideMyLocation();
                        break;

                }

            }

            //打开了条目，但是还没选
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sate_map:
                //卫星地图
                bdMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                break;
            case R.id.btn_normal_map:
                //普通地图
                bdMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                break;
            case R.id.iv_search://搜索
                if (mCurLocation == null) {
                    return;
                }
                String searchKey = et_key.getText().toString().trim();
                if (TextUtils.isEmpty(searchKey)) {
                    Toast.makeText(MainActivity.this, "请输入搜索关键字", Toast.LENGTH_SHORT).show();
                    return;
                }
                //  第四步，发起检索请求；
                PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption();
                nearbySearchOption.keyword(searchKey);
                nearbySearchOption.location(new LatLng(mCurLocation.getLatitude(), mCurLocation.getLongitude()));
                nearbySearchOption.pageCapacity(20)//设置每页容量，默认为每页10条
                        //.pageNum(0)//分页编号
                        .radius(10 * 1000)//设置检索的半径范围
                        .sortType(PoiSortType.distance_from_near_to_far);//搜索结果排序规则，从近到远
                mPoiSearch.searchNearby(nearbySearchOption);
              /*  mPoiSearch.searchInCity((new PoiCitySearchOption())
                        .city("北京")
                        .keyword("美食")
                        .pageNum(10));*/
                et_key.setText("");
                break;
            case R.id.img_location://定位
                if (mCurLocation != null) {
                    LatLng latLng = new LatLng(mCurLocation.getLatitude(), mCurLocation.getLongitude());
                    MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng); //newLatLngZoom(latLng);//12为缩放级别
                    //此方法无动画效果
                    //  bdMap.setMapStatus(mapStatusUpdate);
                    bdMap.animateMapStatus(mapStatusUpdate, 1000);

                    if (markerOverlay != null && markerOverlay.isVisible()) {
                        markerOverlay.remove();
                    }
                    if (textOverlay != null && textOverlay.isVisible()) {
                        textOverlay.remove();
                    }
                    addText(mCurLocation.getLatitude(), mCurLocation.getLongitude());
                    addMarker(mCurLocation.getLatitude(), mCurLocation.getLongitude());
                    showMyLocation();
                }

                break;
            case R.id.iv_baidu://调用百度地图
                if (mCurMarker != null) {
                    //设置对WebRoute进行支持，本地有安装百度地图,会调用打开，没有则打开网页显示查找路线
                    BaiduMapRoutePlan.setSupportWebRoute(true);
                    RouteParaOption routeParaOption = new RouteParaOption();
                    routeParaOption.cityName(mCurLocation.getCity());//公交必须指定城市
                    //路线起始点
                    routeParaOption.startPoint(new LatLng(mCurLocation.getLatitude(), mCurLocation.getLongitude()));
                    //路线结束点
                    routeParaOption.endPoint(mCurMarker.getPosition());
                    //设置公交路线
                    BaiduMapRoutePlan.openBaiduMapDrivingRoute(routeParaOption, this);
                }
                break;
            case R.id.iv_bus://公交路线
                Log.d("MainActivity", "公交路线");
                TransitRoutePlanOption busOption = new TransitRoutePlanOption();
                PlanNode busPlanNode = PlanNode.withLocation(new LatLng(mCurLocation.getLatitude(), mCurLocation.getLongitude()));
                busOption.city(mCurLocation.getCity());//公交必须指定城市
                busOption.from(busPlanNode);
                busOption.to(PlanNode.withLocation(mCurMarker.getPosition()));
                routePlanSearch.transitSearch(busOption);
                break;
            case R.id.iv_car://驾车路线
                Log.d("MainActivity", "驾车路线");
                DrivingRoutePlanOption driverOtion = new DrivingRoutePlanOption();
                PlanNode driverplanNode = PlanNode.withLocation(new LatLng(mCurLocation.getLatitude(), mCurLocation.getLongitude()));
                driverOtion.from(driverplanNode);
                driverOtion.to(PlanNode.withLocation(mCurMarker.getPosition()));
                // driverOtion.passBy();//驾车中途需要经过哪些点
                // driverOtion.policy();//驾车策略
                routePlanSearch.drivingSearch(driverOtion);
                break;
            case R.id.iv_walk://步行路线
                Log.d("MainActivity", "步行路线");
                WalkingRoutePlanOption walkOption = new WalkingRoutePlanOption();
                PlanNode awalkPlanNode = PlanNode.withLocation(new LatLng(mCurLocation.getLatitude(), mCurLocation.getLongitude()));
                walkOption.from(awalkPlanNode);
                walkOption.to(PlanNode.withLocation(mCurMarker.getPosition()));
                routePlanSearch.walkingSearch(walkOption);//进入监听器
                break;
            case R.id.iv_share:
                LocationShareURLOption shareURLOption = new LocationShareURLOption();
                //shareURLOption.location(new LatLng(mCurLocation.getLatitude(),mCurLocation.getLongitude()));
                shareURLOption.location(mCurMarker.getPosition());
                shareURLOption.name("位置分享");
                shareURLOption.snippet("来自小东地图分享");
                mShareUrlSearch.requestLocationShareUrl(shareURLOption);//进入监听位置分享方法
                break;

        }

    }

    private void hideMyLocation() {
        if (bdMap!=null) {
            bdMap.setMyLocationEnabled(false);
        }

    }

    private void showMyLocation() {
        if (mCurLocation != null) {
            MyLocationData.Builder builder = new MyLocationData.Builder();
            MyLocationData data = builder.accuracy(mCurLocation.getRadius())//设置精度范围
                    .latitude(mCurLocation.getLatitude())
                    .longitude(mCurLocation.getLongitude())
                    .direction(mCurLocation.getDirection())//设置方向
                    .build();
            bdMap.setMyLocationEnabled(true);//顺序先设置显示，在设置data
            bdMap.setMyLocationData(data);
        }
    }

    private void removeText() {
        textOverlay.remove();
    }


    private void addText(double latitude, double lontitude) {
        //定义文字所显示的坐标点
        LatLng llText = new LatLng(latitude + 0.0005, lontitude + 0.0005);
        //构建文字Option对象，用于在地图上添加文字
        OverlayOptions textOption = new TextOptions()
                .bgColor(0x00FFFF00)
                .fontSize(300)
                .fontColor(0x50FF00FF)
                .text("小东地图")
                .rotate(-30)
                .position(llText);
        //在地图上添加该文字对象并显示
        //overlay是一个添加的标记
        textOverlay = bdMap.addOverlay(textOption);
    }

    private void removeMarker() {
        // bdMap.clear();//移除地图上所有标记
        markerOverlay.remove();
    }

    private void addMarker(double latitude, double lontitude) {
        //定义Maker坐标点
        LatLng point = new LatLng(latitude, lontitude);//纬度经度
        //构建Marker图标

        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.jiaodian);
        BitmapDescriptor bitmap2 = BitmapDescriptorFactory.fromResource(R.drawable.jiaodian_white);
        ArrayList<BitmapDescriptor> list = new ArrayList<>();
        list.add(bitmap);
        list.add(bitmap2);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icons(list);//添加多个图标
        //.icon(bitmap);//添加一个图标
        //在地图上添加Marker，并显示

        markerOverlay = bdMap.addOverlay(option);
    }

    class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (isFirst) {
                //让地图中心点跑到我的定位的位置
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng, 15);//12为缩放级别
                //此方法无动画效果
                //  bdMap.setMapStatus(mapStatusUpdate);
                bdMap.animateMapStatus(mapStatusUpdate, 1000);//动画效果
                if (markerOverlay != null && markerOverlay.isVisible()) {
                    markerOverlay.remove();
                }
                addMarker(location.getLatitude(), location.getLongitude());
                if (textOverlay != null && textOverlay.isVisible()) {
                    textOverlay.remove();
                }
                addText(location.getLatitude(), location.getLongitude());

                isFirst = false;
            }
            L.d(location.getLatitude()+"-----"+location.getLongitude());
            mCurLocation = location;
            tv_location.setText(location.getAddrStr());

        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.stop();//停止定位
        }
        if (mPoiSearch != null) {
            mPoiSearch.destroy();
        }

        if (routePlanSearch != null) {
            routePlanSearch.destroy();
        }

        if (mShareUrlSearch != null) {
            mShareUrlSearch.destroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
}
